<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                template="../../Templates/SubFolderTemplate.xhtml"
                xmlns:p="http://primefaces.prime.com.tr/ui"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core">
    <ui:define name="head-title">
        <title>View Credit Account | Merlion's ERP System</title>
    </ui:define>
    
    
    <ui:define name="content">
        <f:view beforePhase="#{viewCreditAccountManagerBean.initView}">
            <p:panel id="panel" header="View Credit Account"> 
                <h:form prependId="false" id="form">
                    <p:ajaxStatus style="width:16px;height:16px;">   
                        <f:facet name="start">   
                            <p:graphicImage value="http://www.primefaces.org:8080/showcase/design/ajaxloading.gif" /> 
                        </f:facet> 

                        <f:facet name="complete">   
                            <h:outputText value="" />   
                        </f:facet>   
                    </p:ajaxStatus> 

                    <p:messages /> 


                        <h:outputLabel for="customerName" style="font-weight: bold" value="Search Company : " /> 
                        <p:autoComplete value="#{viewCreditAccountManagerBean.saleLeadName}" required="true"
                                        id="customerName" label="customerName" requiredMessage="Company Name is required." completeMethod="#{viewCreditAccountManagerBean.complete}"/>
                            <p:message for="customerName" /> 

                    <p:fieldset legend="Customer Information" rendered="#{viewCreditAccountManagerBean.isExisting}">
                        
                        <h:panelGrid columns="3">
                        <h:outputLabel for="registrationNumber" style="font-weight: bold" value="Registration Number: " /> 
                        <h:outputText id="registrationNumber" value="#{viewCreditAccountManagerBean.selectedCustomer.registrationNumber}" rendered="#{viewCreditAccountManagerBean.isExisting}"/> 
                        <p:message for="registrationNumber" /> 

                        <h:outputLabel for="creditAccountId" style="font-weight: bold" value="Credit Account ID: " /> 
                        <h:outputText id="creditAccountId" value="#{viewCreditAccountManagerBean.selectedCustomer.creditAccount.creditAccountId}" rendered="#{viewCreditAccountManagerBean.isExisting}"/>                         
                        <p:message for="creditAccountId" /> 

                        <h:outputLabel for="creditLimit" style="font-weight: bold" value="Credit Limit: " /> 
                        <h:outputText id="creditLimit" value="#{viewCreditAccountManagerBean.selectedCustomer.creditAccount.creditLimit}" rendered="#{viewCreditAccountManagerBean.isExisting}"/>                                            
                        <p:message for="creditLimit" />

                        <h:outputLabel for="outstandingCredit" style="font-weight: bold" value="Outstanding Credit: " /> 
                        <h:outputText id="outstandingCredit" value="#{viewCreditAccountManagerBean.selectedCustomer.creditAccount.outstandingCredit}" rendered="#{viewCreditAccountManagerBean.isExisting}"/>                         
                        <p:message for="outstandingCredit" />
                        
                        <h:outputLabel for="creditBalance" style="font-weight: bold" value="Credit Balance: " /> 
                        <h:outputText id="creditBalance" value="#{viewCreditAccountManagerBean.selectedCustomer.creditAccount.creditBalance}" rendered="#{viewCreditAccountManagerBean.isExisting}"/>                         
                        <p:message for="creditBalance" />
                        </h:panelGrid>
                    </p:fieldset>
                            
                    <br></br>

                    <p:fieldset legend="Sale Order Information" rendered="#{viewCreditAccountManagerBean.isExisting}" >
                    <p:tabView style="display: inline-block; width:99%" >
                        <p:tab title="Pending">
                            <p:dataTable var ="saleOrder" value="#{viewCreditAccountManagerBean.pending}" 
                                         rows="5" emptyMessage="No Sale Order found" selection="#{viewCreditAccountManagerBean.selectedSaleOrder}" >
                                <p:column selectionMode="single"/>

                                <p:column sortBy="#{saleOrder.id}" filterBy="#{saleOrder.id}"> 
                                    <f:facet name="header"> 
                                        <h:outputText value="Sale Order ID" /> 
                                    </f:facet> 
                                    <h:outputText value="#{saleOrder.id}" /> 
                                </p:column> 

                                <p:column sortBy="#{saleOrder.totalPrice}" filterBy="#{saleOrder.totalPrice}"> 
                                    <f:facet name="header"> 
                                        <h:outputText value="Total Price" /> 
                                    </f:facet> 
                                    <h:outputText value="#{saleOrder.totalPrice}" /> 
                                </p:column> 

                                <p:column sortBy="#{saleOrder.customer.saleLeadName}" filterBy="#{saleOrder.customer.saleLeadName}"> 
                                    <f:facet name="header"> 
                                        <h:outputText value="Company Name" /> 
                                    </f:facet> 
                                    <h:outputText value="#{saleOrder.customer.saleLeadName}" /> 
                                </p:column>

                                <p:column sortBy="#{saleOrder.stateSaleOrder}" filterBy="#{saleOrder.stateSaleOrder}"> 
                                    <f:facet name="header"> 
                                        <h:outputText value="Status" /> 
                                    </f:facet> 
                                    <h:outputText value="#{saleOrder.stateSaleOrder}" /> 
                                </p:column>


                                <f:facet name="footer">
                                    <p:commandButton value="View" image="ui-icon ui-icon-search" update="display1 display2" oncomplete="saleOrderViewDialog.show()"/>                        
                                    <p:commandButton value="Make Cash In Advance" actionListener="#{viewCreditAccountManagerBean.makeCashInAdvance(event)}" ajax="true">
                                        <f:setPropertyActionListener value="#{saleOrder}" target="#{viewCreditAccountManagerBean.selectedSaleOrder}"/>
                                    </p:commandButton>
                                    <p:commandButton value="Pay existing invoice" update="panel" actionListener="#{viewCreditAccountManagerBean.payExistingInvoice}" ajax="true" />
                                    <p:commandButton value="Reduce Order" update="panel" actionListener="#{viewCreditAccountManagerBean.reduceOrder}" ajax="true" />                
                                </f:facet>
                            </p:dataTable>
                        </p:tab>

                    <p:tab title="Confirmed">
                        <p:dataTable var ="saleOrder" value="#{viewCreditAccountManagerBean.confirmed}" 
                                         rows="5" emptyMessage="No Sale Order found">

                                <p:column sortBy="#{saleOrder.id}" filterBy="#{saleOrder.id}"> 
                                <f:facet name="header"> 
                                    <h:outputText value="Sale Order ID" /> 
                                </f:facet> 
                                <h:outputText value="#{saleOrder.id}" /> 
                            </p:column> 

                                <p:column sortBy="#{saleOrder.totalPrice}" filterBy="#{saleOrder.totalPrice}"> 
                                <f:facet name="header"> 
                                    <h:outputText value="Total Price" /> 
                                </f:facet> 
                                <h:outputText value="#{saleOrder.totalPrice}" /> 
                            </p:column> 

                            <p:column sortBy="#{saleOrder.customer.saleLeadName}" filterBy="#{saleOrder.customer.saleLeadName}"> 
                                <f:facet name="header"> 
                                    <h:outputText value="Company Name" /> 
                                </f:facet> 
                                <h:outputText value="#{saleOrder.customer.saleLeadName}" /> 
                            </p:column>

                            <p:column sortBy="#{saleOrder.stateSaleOrder}" filterBy="#{saleOrder.stateSaleOrder}"> 
                                <f:facet name="header"> 
                                    <h:outputText value="Status" /> 
                                </f:facet> 
                                <h:outputText value="#{saleOrder.stateSaleOrder}" /> 
                            </p:column>
                            </p:dataTable> 
                    </p:tab>
                    </p:tabView>
                        
                </p:fieldset>
                
                <p:commandButton value="Retrieve Information" actionListener="#{viewCreditAccountManagerBean.retrieve}" update="panel"/> 
                
                <p:dialog header="Sale Order Detail" widgetVar="saleOrderViewDialog" width="600" height="400" resizable="true" showEffect="clip" hideEffect="fold">
                    <h:panelGrid id="display1" columns="2" cellpadding="4">

                        <h:outputText value="SaleOrder ID:"/>
                        <h:outputText value="#{viewCreditAccountManagerBean.selectedSaleOrder.id}"/>

                        <h:outputText value="Date:"/>
                        <h:outputText value="#{viewCreditAccountManagerBean.selectedSaleOrder.saleOrderDate}"/>

                        <h:outputText value="Company Name:"/>
                        <h:outputText value="#{viewCreditAccountManagerBean.selectedSaleOrder.customer.saleLeadName}"/>

                    </h:panelGrid>

                    <p:dataTable var="lineItem" value="#{viewCreditAccountManagerBean.selectedSaleOrder.lineItems}"
                                 dynamic="true" paginator="true" emptyMessage="No Product Type" id="display2">
                        <p:column sortBy="#{lineItem.productPrice.productType}">
                            <f:facet name="header">
                                <h:outputText value="Type"/>
                            </f:facet>
                            <h:outputText value="#{lineItem.productPrice.productType}"/>
                        </p:column>

                        <p:column sortBy="#{lineItem.lineItemQuantity}">
                            <f:facet name="header">
                                <h:outputText value="Quantity"/>
                            </f:facet>
                            <h:outputText value="#{lineItem.lineItemQuantity}"/>
                        </p:column>

                        <p:column sortBy="#{lineItem.productPrice.productPrice}">
                            <f:facet name="header">
                                <h:outputText value="Price per Unit"/>
                            </f:facet>
                            <h:outputText value="#{lineItem.productPrice.productPrice}"/>
                        </p:column>

                        <p:column sortBy="#{lineItem.finalPrice}">
                            <f:facet name="header">
                                <h:outputText value="Final Price"/>
                            </f:facet>
                            <h:outputText value="#{lineItem.finalPrice}"/>
                        </p:column>

                        <f:facet name="footer">
                            <h:outputText value="Total Price: "/>
                            <h:outputText value="#{viewCreditAccountManagerBean.selectedSaleOrder.totalPrice}"/>                        
                        </f:facet>
                     </p:dataTable>

                </p:dialog> 
                </h:form>
                
        </p:panel> 
        </f:view>
        
    </ui:define>

</ui:composition>
